﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceability_2203.Common.DBContexts;
using Traceability_2203.Common.Infrastructure.Interfaces;

namespace Traceability_2203.Common.Infrastructure.Repositories
{
    /// <summary>
    /// 基类仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
    {
        /// <summary>
        /// EF上下文
        /// </summary>
        private readonly EFDBContext dbcontext;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbcontext">EF上下文</param>
        public BaseRepository(EFDBContext dbcontext)
        {
            this.dbcontext = dbcontext;
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<IQueryable<T>> GetAll()
        {
            var result = dbcontext.Set<T>();
            return result;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            dbcontext.Set<T>().Add(entity);
            return dbcontext.SaveChanges();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<int> Upd(T entity)
        {
            dbcontext.Set<T>().Update(entity);
            return dbcontext.SaveChanges();
        }
    }
}
